from PyQt6.QtWidgets import QFileDialog,QMessageBox,QWidget,QHBoxLayout,QCheckBox,QSpacerItem,QLabel,QVBoxLayout
from PyQt6.QtWidgets import QApplication,QSizePolicy,QPushButton
from PyQt6.QtCore import QSize,Qt
from PyQt6.QtGui import QIcon,QPalette,QColor
import os
from utils.Logger import LogPrint
from views.MultiChoiceListView import MultiChoiceListWidget
from utils import ImageUtils


class LeftFileListAreaView(QWidget):


    def __init__(self,parent=None):
        super().__init__(parent=parent)

        self.onSelectedFileChangeListener=None

        self.setupUi()


    def setupUi(self):

        # 设置 QWidget 的背景色
        palette = self.palette()
        palette.setColor(QPalette.ColorRole.Window, QColor("white"))
        self.setPalette(palette)
        self.setAutoFillBackground(True)

        #self.setStyleSheet("QWidget { background-color: yellow; }")

        backgroundLayout=QVBoxLayout()

        #左边布局
        backgroundLayout=QVBoxLayout()
        backgroundLayout.setAlignment(Qt.AlignmentFlag.AlignTop)

        #左边，顶部的全选按钮
        layout=QHBoxLayout()
        layout.setAlignment(Qt.AlignmentFlag.AlignLeft)
        self.cbSelectAll=QCheckBox('全选')
        self.cbSelectAll.setFixedSize(QSize(60,30))
        self.cbSelectAll.clicked.connect(self.onClickCbSelectAll)
        layout.addWidget(self.cbSelectAll)
        self.labelFileCount=QLabel('0/0')
        self.labelFileCount.setFixedSize(QSize(30,30))
        layout.addWidget(self.labelFileCount)
        backgroundLayout.addLayout(layout)

        #左边，中部列表
        self.fileListView=MultiChoiceListWidget()
        self.fileListView.setItemClickedCallback(self.onListviewItemClicked)
        backgroundLayout.addWidget(self.fileListView)

        #左边，底部按钮
        layout=QHBoxLayout()
        layout.setAlignment(Qt.AlignmentFlag.AlignHCenter)
        btnAddFile=QPushButton('增加文件')
        btnAddFile.setFixedSize(QSize(80,30))
        btnAddFile.setIcon(ImageUtils.getQIcon('icon_add.svg'))
        btnAddFile.clicked.connect(self.onClickedBtnAddFile)
        layout.addWidget(btnAddFile)
        backgroundLayout.addLayout(layout)
        btnClean=QPushButton('清空文件')
        btnClean.setFixedSize(QSize(80,30))
        btnClean.setIcon(ImageUtils.getQIcon('icon_delete.svg'))
        btnClean.clicked.connect(self.onClickedBtnCleanFile)
        layout.addWidget(btnClean)
        backgroundLayout.addLayout(layout)
        
        self.setLayout(backgroundLayout)

    def setOnSelectedFileChangeListener(self,onSelectedFileChangeListener):
        self.onSelectedFileChangeListener=onSelectedFileChangeListener

    #首次打开界面时，设置列表数据
    def setListItem(self,filePath):
        self.fileListView.clear()
        self.fileListView.addItem(filePath)
        self.fileListView.setSelectAll()
        self.cbSelectAll.setChecked(True)
        self.labelFileCount.setText('1/1')

    #选中所有
    def onClickCbSelectAll(self,event):
        LogPrint.info('onClickCbSelectAll',event)
        total,selectCount=self.fileListView.getItemCount()
        if total <=0:
            QMessageBox.warning(None,'系统提示','未添加任何文件')
            return
        if event:
            self.fileListView.setSelectAll()
        else :
            self.fileListView.setClearSelection()

        total,selectCount=self.fileListView.getItemCount()
        self.labelFileCount.setText(f'{selectCount}/{total}')

        if self.onSelectedFileChangeListener:
            selectedPdfList=self.fileListView.getSelectItems()
            self.onSelectedFileChangeListener(selectedPdfList,selectedPdfList[0])

    #左侧文件列表项选中时处理逻辑
    def onListviewItemClicked(self,selectItem=None):
        LogPrint.info('onListviewItemClicked',selectItem)
        #更新已选的文件列表
        selectedPdfList=self.fileListView.getSelectItems()
        latestSelectPdfPath=selectItem
        if self.onSelectedFileChangeListener:
            self.onSelectedFileChangeListener(selectedPdfList,latestSelectPdfPath)
        #更新全选状态以及数量
        total,selectCount=self.fileListView.getItemCount()
        self.labelFileCount.setText(f'{selectCount}/{total}')
        if total==selectCount:
            self.cbSelectAll.setChecked(True)
        else:
            self.cbSelectAll.setChecked(False)


     #主界面底部，增加文件
    def onClickedBtnAddFile(self,event):
        total,selectCount=self.fileListView.getItemCount()
        if total >=5:
            QMessageBox.warning(None,'系统提示','最多添加5个文件')
            return
        desktopPath=os.path.join(os.path.expanduser("~"),'Desktop')
        if desktopPath  is None:
            desktopPath='c:/'
        # 使用 QFileDialog 打开一个目录对话框，并获取选择的目录路径
        filepath = QFileDialog.getOpenFileName(self, "选择文件", desktopPath,"*.pdf")
        if filepath and filepath[0] != '':
            LogPrint.info(filepath)  # 打印选择的目录路径
            self.fileListView.addItem(filepath[0])

    #清空文件
    def onClickedBtnCleanFile(self,event):
        total,selectCount=self.fileListView.getItemCount()
        if total <=0:
            QMessageBox.warning(None,'系统提示','没有选择任何文件')
            return
        self.fileListView.clear()

        self.labelFileCount.setText('0/0')

        if self.onSelectedFileChangeListener:
            self.onSelectedFileChangeListener(None,None)


    